# 存放项目中一些公共函数
import functools

from flask import current_app
from flask import session

from info.models import User


def do_index_class(num):
    """自定义函数用来过滤类名， 点击排行的新闻的类名"""
    if num == 1:
        return "first"
    elif num == 2:
        return "second"
    elif num == 3:
        return "third"
    else:
        return ""


# 装饰器装饰函数获取登录用户信息
from flask import g
def user_login_data(view_func):
    @functools.wraps(view_func)
    def wrapper(*args, **kwargs):
        # 获取用户登录信息
        # 获取到当前登录用户的id
        user_id = session.get("user_id")
        # 通过id获取用户信息
        user = None
        if user_id:
            try:
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)

        g.user = user
        return view_func(*args, **kwargs)
    return wrapper